/**
 * File.java
 */
package dataTypes;

import model.Model;

/**
 * File descriptor, contains file local path, size, numberOfParts and hash.
 * Files are equal if their size and hash equals.
 * @author Michal Kolodziejski
 *
 */
public class File {

    /**System independent separator*/
    public static final String separator = java.io.File.separator;
    protected String path;
    protected long size; // size in bytes
    protected int numberOfParts; // = size/partSize
    protected String hash;

    /**
     * Default constructor
     */
    public File() {
    }

    /**
     * Copying constructor
     */
    public File(File f) {
        path = f.path;
        size = f.size;
        numberOfParts = f.numberOfParts;
        hash = f.hash;
    }

    /**
     * Parametrized constructor
     * @param path a path
     * @param size a size
     * @param hash a MD5 hash
     */
    public File(String path, long size, String hash) {
        this.path = path;
        this.size = size;
        // set numberOfParts
        this.numberOfParts = (int) ((size + Model.partSize - 1) / Model.partSize);
        this.hash = hash;
    }

    /**
     * @return the hash
     */
    public String getHash() {
        return hash;
    }

    /**
     * @return the numberOfParts
     */
    public int getNumberOfParts() {
        return numberOfParts;
    }

    /**
     * @return the path
     */
    public String getPath() {
        return path;
    }

    /**
     * @return the size
     */
    public long getSize() {
        return size;
    }

    /**
     * @param hash the hash to set
     */
    public void setHash(String hash) {
        this.hash = hash;
    }

    /**
     * @param path the path to set
     */
    public void setPath(String path) {
        this.path = path;
    }

    /**
     * @param size the size to set
     */
    public void setSize(long size) {
        this.size = size;
        // set numberOfParts
        this.numberOfParts = (int) ((size + Model.partSize - 1) / Model.partSize);
    }

    /**
     * Compares two Files. Files are equal if their size and hash are the same.
     */
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof File)) {
            return false;
        }
        File f = (File) obj;
        if (this == f) {
            return true;
        }
        if (this.hash == null || f.hash == null) {
            return false;
        }
        if (this.size == f.size && this.hash.equals(f.hash)) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder toStringBuilder = new StringBuilder();
        toStringBuilder.append(super.toString());
        toStringBuilder.append("\n");
        toStringBuilder.append("\npath: ");
        toStringBuilder.append(path);
        toStringBuilder.append("\nsize: ");
        toStringBuilder.append(size);
        toStringBuilder.append("\nnumberOfParts: ");
        toStringBuilder.append(numberOfParts);
        toStringBuilder.append("\nhash: ");
        toStringBuilder.append(hash);
        return toStringBuilder.toString();
    }
}
